﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.ShprossesBuilder.Properties;
using ITAP.Core.Modelling;
using ITAP.Core.Calculations;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationAutoRotateShprossConnectors : IOperation
    {
        #region IOperation Members

        public string Name
        {
            get { return "Автоповорот соединителей"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossConnectorOperation; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.AutoRotateShprossConnectors.ToBitmap(); }
        }

        private void Fix(List<ShprossConnector> shprossConnectors)
        {
            foreach (ShprossConnector shprossConnector in shprossConnectors)
            {
                if (shprossConnector.IsConnectorsAnglesCorrect())
                    continue;

                int connectorIter = 0;
                Angle angleToRotate = null;
                Angle prevAngleToRotate = null;
                double averageAngle = 0;
                foreach (IComponent innerComponent in shprossConnector.InnerComponents)
                {
                    if (innerComponent.GetType() == typeof(Connector))
                    {
                        Connector connector = innerComponent as Connector;
                        ModelVector shprossConnectorTangent = ModelCalculations.RotateVector(new ModelVector(1, 0), shprossConnector.Angle.Value);
                        Angle connectorAngle = ModelCalculations.GetAngleBetweenVectors(shprossConnectorTangent, connector.Primitives[0].GetTangent(0), false);
                        AngleInterval interval = shprossConnector.AllowedConnectorsAngles[connectorIter];
                        angleToRotate = interval.MiddleAngle - connectorAngle;
                        if (prevAngleToRotate != null &&
                            !SimpleEqualProvider.IsEqual(angleToRotate.Value, prevAngleToRotate.Value, 0.01))
                            return;
                        prevAngleToRotate = angleToRotate.Clone() as Angle;
                        averageAngle += angleToRotate.Value;
                        connectorIter++;
                    }
                }
                shprossConnector.Rotate(-averageAngle / connectorIter);
            }
        }

        public void Execute(ModelControl modelControl)
        {
            List<ShprossConnector> shprossConnectors = new List<ShprossConnector>();
            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
                if (selectedComponent is ShprossConnector)
                    shprossConnectors.Add(selectedComponent as ShprossConnector);
            this.Fix(shprossConnectors);
            modelControl.UpdateModel();
            this.Fix(shprossConnectors);
            modelControl.UpdateModel();
            this.Fix(shprossConnectors);
            modelControl.UpdateModel();
        }

        public bool CanUndo
        {
            get { return true; }
        }

        #endregion
    }
}
